package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.google.android.material.snackbar.Snackbar;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.adapter.FragmentStareAdapter;
import com.tianxin.workbao.workBao.fragment.CoalFragment;
import com.tianxin.workbao.workBao.holder.SearchViewHolder;

/**
 * Created by hacker on 2017/7/26.
 */

public class CoalStorageActivity extends BaseActivity implements View.OnClickListener {
    private ViewPager mViewPager;
    private FragmentStareAdapter mAdapter;
    private SearchViewHolder.OnSearchHandlerListener searchHandlerListener;
    private FloatingActionButton topFab;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coal_storage);
        super.initStatusBar(true);

        initView();
        initData();
        initEvent();
        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("煤种库存");
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        setSupportActionBar(mToolbar);

        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(1);
        mViewPager.setCurrentItem(0);
        topFab = (FloatingActionButton) findViewById(R.id.top_fab);
    }

    @Override
    public void initData() {
        mFragments.add(CoalFragment.newInstance());
        mAdapter = new FragmentStareAdapter(getSupportFragmentManager(), mFragments, null);
    }

    @Override
    public void initEvent() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CoalStorageActivity.this.finish();
            }
        });
        topFab.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        mViewPager.setAdapter(mAdapter);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        switch (id) {
            case R.id.search:
                showSearchView(this, getSearchHandlerListener());
                break;

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //搜索接口
    public SearchViewHolder.OnSearchHandlerListener getSearchHandlerListener() {
        if (searchHandlerListener == null) {
            searchHandlerListener = new SearchViewHolder.OnSearchHandlerListener() {
                @Override
                public void onSearch(int code) {
                    switch (code) {
                        case SearchViewHolder.RESULT_SEARCH_EMPTY_KEYWORD:
                            Snackbar.make(getWindow().getDecorView(), R.string.keyword_is_empty, Snackbar.LENGTH_SHORT).show();
                            break;
                        case SearchViewHolder.RESULT_SEARCH_SEARCH:
                            String query = mSearchViewHolder.et_search_content.getText().toString();
                            mFragments.get(0).search(query);
                            break;
                        case SearchViewHolder.RESULT_SEARCH_GO_SCAN:

                            break;
                        case SearchViewHolder.RESULT_SEARCH_CANCEL:
                            mPopupWindow.dismiss();
                            break;
                    }
                }
            };
        }
        return searchHandlerListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.top_fab:
                mFragments.get(0).initData(false);
                break;
        }
    }
}
